package com.ahdy.mapper;

import com.ahdy.entity.MedicationPlan;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

// 6. 用药计划Mapper
@Mapper
public interface MedicationPlanMapper {

    @Insert("INSERT INTO medication_plans(user_id, medication_id, dosage, frequency_per_day, timing_type, " +
            "specific_time, duration_days, special_instructions, created_at, updated_at) " +
            "VALUES(#{userId}, #{medicationId}, #{dosage}, #{frequencyPerDay}, #{timingType}, " +
            "#{specificTime}, #{durationDays}, #{specialInstructions}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MedicationPlan plan);

    @Update("UPDATE medication_plans SET dosage=#{dosage}, frequency_per_day=#{frequencyPerDay}, " +
            "timing_type=#{timingType}, specific_time=#{specificTime}, duration_days=#{durationDays}, " +
            "special_instructions=#{specialInstructions}, updated_at=#{updatedAt} WHERE id=#{id}")
    int update(MedicationPlan plan);

    @Delete("DELETE FROM medication_plans WHERE id=#{id}")
    int deleteById(Integer id);

    @Select("SELECT * FROM medication_plans WHERE id=#{id}")
    MedicationPlan findById(Integer id);

    @Select("SELECT * FROM medication_plans WHERE user_id=#{userId} ORDER BY specific_time ASC")
    List<MedicationPlan> findByUserId(Integer userId);

    @Select("SELECT * FROM medication_plans WHERE user_id=#{userId} AND timing_type=#{timingType} ORDER BY specific_time ASC")
    List<MedicationPlan> findByUserIdAndTimingType(@Param("userId") Integer userId, @Param("timingType") MedicationPlan.TimingType timingType);

    @Delete("DELETE FROM medication_plans WHERE user_id=#{userId}")
    int deleteByUserId(Integer userId);

    // 获取用药计划详情（包含药物信息）
    List<Map<String, Object>> findPlanDetailsWithMedication(Integer userId);

    // 检查用药冲突
    List<MedicationPlan> findConflictingPlans(@Param("userId") Integer userId, @Param("timingType") MedicationPlan.TimingType timingType,
                                              @Param("specificTime") String specificTime);
}
